package com.xiaobaibai.server.timeline;

import com.xiaobaibai.dao.TimeLineDao;
import com.xiaobaibai.pojo.TimeLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TimeServiceImpl implements ITimeLineService {

    @Autowired
    private TimeLineDao timeLineDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<TimeLine> getTimeline() {
        return timeLineDao.getTimelineList();
    }

    @Override
    public TimeLine addTimeline(String content) {
        TimeLine timeLine=new TimeLine();
        timeLine.setCreatedTime(new Date());
        timeLine.setTimelineContent(content);
        timeLineDao.insertSelective(timeLine);
        //还需要更新缓存,清除了就重新从数据库中获取
        stringRedisTemplate.delete("yearList");
        return timeLine;
    }

    @Override
    public void delTimeline(Integer id) {
        timeLineDao.deleteByPrimaryKey(id);
        stringRedisTemplate.delete("yearList");
    }
}
